package com.zzptc.sky.bdsjws;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import com.socks.library.KLog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zzptc.sky.bdsjws.activites.base.BaseActivity;
import com.zzptc.sky.bdsjws.com.Constants;
import com.zzptc.sky.bdsjws.db.BbxDao;
import com.zzptc.sky.bdsjws.db.DaoMaster;
import com.zzptc.sky.bdsjws.db.DaoSession;
import com.zzptc.sky.bdsjws.utils.MyUtil;
import com.zzptc.sky.bdsjws.vo.Bbx;

import org.greenrobot.greendao.database.Database;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by Administrator on 2017/4/20.
 */

public class MyApplication extends Application{
    //功能：提供全局的上下文
    static Context context;
    //真正的入口方法
    static DaoSession daoSession;
    static DaoSession    mobiledaoSession;
    public void onCreate() {
        super.onCreate();
        KLog.init(BuildConfig.LOG_DEBUG, Constants.LOG_FLAG);
        context=getApplicationContext();
        initClient();
        KLog.init(BuildConfig.LOG_DEBUG,Constants.LOG_FLAG);
        //耗时操作  放入线程
        initDatabase();
        initDatabasel();
        new Thread(new Runnable() {
            @Override
            public void run() {
                initDatatoTable();
                //将外部数据库拷贝到项目中 拷贝到手机储存器中

                try {
                    MyUtil.copyFileToMobile(context,"mobile.db","mobile.db");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initClient() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();


        OkHttpUtils.initClient(okHttpClient);

    }

    public static Context getContext(){

        return context;
    }
    //初始化GreenDao
    public void initDatabase(){
       DaoMaster.DevOpenHelper openHelper=new DaoMaster.DevOpenHelper(context,Constants.DATA_BASE_NAME);
        Database database=openHelper.getWritableDb();
        DaoMaster daoMaster=new DaoMaster(database);
        daoSession=daoMaster.newSession();
        /**
         * BBX
         * @return
         */


    }
    public static DaoSession getDaoSession(){
        return daoSession;
    }

    /**
     * 外部数据库的
     */
    public void initDatabasel(){
        DaoMaster.DevOpenHelper openHelper=new DaoMaster.DevOpenHelper(context,"mobile.db");
        Database database=openHelper.getWritableDb();
        DaoMaster daoMaster=new DaoMaster(database);
        mobiledaoSession=daoMaster.newSession();
    }
    public static DaoSession getMobiledaoSession(){

        return mobiledaoSession;
    }
    //将数据新增到表   只能操作一次  共享首选项
    public void initDatatoTable(){
        DaoSession daoSession=getDaoSession();
        BbxDao bbxDao=daoSession.getBbxDao();
        try {
            MyUtil.copyFileToMobile(context,"mobile.db","aaa.db");
        } catch (Exception e) {
            e.printStackTrace();
        }
        SharedPreferences sharedPreferences= MyUtil.getSharedPre();
        boolean b=sharedPreferences.getBoolean("inserttable",false);
        //控制只初始化一次
        if(!b){
        //数据源在哪   Strig.xml
        String[] names=getResources().getStringArray(R.array.bbx_name);
        String[] icons=getResources().getStringArray(R.array.bbx_icon);
        for(int i=0;i<names.length;i++) {
            Bbx bbx = new Bbx(null, names[i], icons[i]);
            try {
                long m = bbxDao.insert(bbx);
                if (m > 0) {
                    KLog.i("新增成功");
                } else {
                    KLog.i("新增失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
            //新增完毕，将共享首选项中的值改为true
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putBoolean("inserttable",true);
            editor.commit();
        }
    }
}
